package zhongbo.java.air.emulator;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.nio.charset.Charset;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月03日 星期四 09:24:36
 */
@Component
@Slf4j
public class RedisFlightPathPublisher {

    @Value("${traceDataPath:}")
    private String traceDataPath;

    private final String PUBLISH_CHANEL = "flight_path_chanel";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostConstruct
    public void publishPath() {
        //File dir = new File(traceDataPath+"trace_full_acb84d.json");
        File file = new File(traceDataPath + "/trace_full_acb84d.json");
        //File[] files = dir.listFiles();
        //File file = files[0];
        String readString = FileUtil.readString(file, Charset.defaultCharset());
        JSONObject jsonObject = JSONUtil.parseObj(readString);
        JSONArray trace = jsonObject.getJSONArray("trace");

        while (true) {
            long now = System.currentTimeMillis();
            for (Object o : trace) {
                JSONArray tracePart = (JSONArray) o;
                JSONObject jo = new JSONObject();
                jo.putOnce("icao", jsonObject.getStr("icao"));
                jo.putOnce("r", jsonObject.getStr("r"));
                jo.putOnce("type", jsonObject.getStr("t"));
                jo.putOnce("dbFlags", jsonObject.getStr("dbFlags"));
                jo.putOnce("desc", jsonObject.getStr("desc"));
                // jo.putOnce("timestamp", jsonObject.getDouble("timestamp"));
                jo.putOnce("timestamp", now);
                jo.putOnce("trace", tracePart);
                ThreadUtil.sleep(1000);
                String jsonString = JSONUtil.toJsonStr(jo);
                log.info("推送航迹数据: {}", jsonString);
                redisTemplate.convertAndSend(PUBLISH_CHANEL, jsonString);
            }
        }
    }

    public static void main(String[] args) {
        File dir = new File("D:\\code\\gitee\\js-air-flights-data\\trace_full_json");
        File[] files = dir.listFiles();
        File file = files[0];
        String readString = FileUtil.readString(file, Charset.defaultCharset());
        JSONObject jsonObject = JSONUtil.parseObj(readString);
        JSONArray trace = jsonObject.getJSONArray("trace");
        JSONObject jo = new JSONObject();
        jo.put("icao", jsonObject.getStr("icao"));
        jo.put("r", jsonObject.getStr("r"));
        jo.putOnce("type", jsonObject.getStr("t"));
        jo.putOnce("dbFlags", jsonObject.getStr("dbFlags"));
        jo.putOnce("desc", jsonObject.getStr("desc"));
        jo.putOnce("timestamp", jsonObject.getDouble("timestamp"));
        jo.putOnce("trace", trace.get(0));

        System.out.println(jo);


    }

}
